package com.ycx.furns.web;

import com.ycx.furns.entity.Member;
import com.ycx.furns.service.MemberService;
import com.ycx.furns.service.impl.MemberServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    MemberService memberService = new MemberServiceImpl();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("user-name");
        String pwd = req.getParameter("user-password");

        Member member = memberService.loginCheck(name, pwd);
        if(member != null){
            System.out.println("用户名密码验证正确，可以登录");
            req.getSession().setAttribute("login_name", name);
            req.getRequestDispatcher("/views/member/login_ok.jsp").forward(req, resp);
        }else {
            req.setAttribute("username", name);
            req.setAttribute("tip", "用户名或密码错误");
            req.getRequestDispatcher("/views/member/login.jsp").forward(req, resp);
        }
    }
}
